package com.forever.user.manager;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.forever.util.CommonField;
import com.forever.util.CommonUtil;

@Component
@SuppressWarnings({ "rawtypes", "unused" })
public class MulitiThreadManager {
	private Logger logger = Logger.getLogger(MulitiThreadManager.class);

	@Autowired
	private JedisPool jedisPool;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private RedisTemplate redisTemplate;

	public void randomGet(int count) {
		for (int i = CommonField.ONE_INT; i <= count; i++) {
			try {
				String id = CommonUtil.getLuckNumber(CommonField.THREE_INT);
				String str = stringRedisTemplate.opsForValue().get(id);
				logger.info("i:" + i + ", id:" + id + ", str:" + str);
				Thread.sleep(CommonField.THREE_HUNDRED_LONG);
				if (i % CommonField.TEN_INT == 0) {
					stringRedisTemplate.opsForValue().set(id, id, CommonField.ONE_LONG, TimeUnit.SECONDS);
					logger.info("key:" + id + ", value:" + id + ", expire:" + CommonField.ONE_LONG + "-"
							+ TimeUnit.SECONDS);
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
				continue;
			}
		}
		Jedis jedis = jedisPool.getResource();
		jedisPool.returnResource(jedis);
	}
}